<?php
namespace app\api\controller;

use think\Request;

class Wdgs extends Base
{   
    /**
     * 配置表
     */
    public function config()
    {
        $res = db('wdgs_config')->where(['admin_id'=>$this->accessData['aid']])->find();
        
        return $res;
    }
    
    
    /**
     * 点击开始(创建个人中心)
     */
    public function PersonalCenter()
    {
        $day = date("Y-m-d");
        $admin_id = $this->accessData['aid'];
        $user_id = $this->accessData['uid'];
        
        //判断是否存在
        $user = db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->find();
        
        //获取配置信息
        $config = $this->config();
        
        if (!$user){
            $data['admin_id'] = $admin_id;
            $data['user_id'] = $user_id;
            $data['create_time'] = date("Y-m-d");
            $data['game_number'] = $config['game_number_day'] + $config['game_number_new'];
            $data['game_wudi_number'] = $config['game_wudi'];
            db('wdgs_center')->insert($data);
            
        }else{
            if ($user['create_time'] != $day){      //如果最新日期和当前日期不一样，增加挑战机会
                db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])
                   ->update(['create_time'=>$day,'game_number'=>$config['game_number_day']]);
            }
        }
        
        //每次访问游戏次数+1
        db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('game_list_number');
        
        //获取用户游戏次数
        $where['admin_id'] = $admin_id;
        $where['user_id'] = $user_id;
        $where['game_number'] = ['gt',0];
        $res = db('wdgs_center')->where($where)->find();
        
        if (!$res){
            $arr['title1'] = $config['game_over_one'];
            $arr['title2'] = $config['game_over_two'];
            $arr['title3'] = $config['game_over_therr'];
            err('您没有挑战机会,请去分享群',$arr);
        }else{
            
            //挑战次数-1
            db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->setDec('game_number');
            suc('ok');
        }
    }
    
    
    /**
     * 挑战失败
     * distance - 挑战米(m)
     */
    public function GameOver()
    {
        $admin_id = $this->accessData['aid'];
        $user_id = $this->accessData['uid'];
        
        $post = request()->only(['distance'],'post');
        
        if(empty($post['distance'])){
            err('挑战参数不能为空');
        }
        
        if ($post['distance'] < 0){
            $post['distance'] = 0;
        }
        
        //剩余无敌卡
        $res = db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->field('distance,game_wudi_number,relationship_id')->find();
        
        if ($post['distance'] > $res['distance']){
            //更新最新分数
            $user = db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->update(['distance'=>$post['distance']]);
        }
        
        //查询即将超过的好友
        $where['a.admin_id'] = $admin_id;
        $where['a.user_id'] = ['neq',$user_id];
        $where['a.relationship_id'] = $res['relationship_id'];
        $where['a.distance'] = ['gt',$post['distance']];
        $where['b.avatarurl'] = ['neq',""];
        $where['b.nickname'] = ['neq',""];
        
        $res['game'] = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')
                        ->where($where)->field('b.avatarurl,a.distance')->order('a.distance asc')->find();
        unset($res['relationship_id']);
                                      
        suc('',$res);
        
        
    }
    
    
    /**
     * 继续挑战
     * type = 1,无敌卡 2，跳过
     */
    public function GoGame()
    {
        
        $admin_id = $this->accessData['aid'];
        $user_id = $this->accessData['uid'];
        
        $post = request()->only(['type'],'post');
        
        if (empty($post['type'])){
            err('类型不能为空');
        }
        
        if ($post['type'] == 1){
            
            //判断无敌卡
            $info = db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->field('game_wudi_number,distance')->find();
            
            if ($info['game_wudi_number'] > 0){
                //无敌卡-1
                db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->setDec('game_wudi_number');
                
                suc('你无敌啦',$info);
            }else{
                err('您无敌卡不足');
            }
        }else{
            
            //获取页面数据
            $res = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')
                    ->where(['a.admin_id'=>$admin_id,'a.user_id'=>$user_id])
                    ->field('b.avatarurl,a.distance')->find();
            
            //计算所有游戏玩家
            $arr = db('wdgs_center')->where(['admin_id'=>$admin_id])->count('id');

            //计算已超过的玩家
            $user = db('wdgs_center')->where(['admin_id'=>$admin_id,'distance'=>['lt',$res['distance']]])->count('id');

            $res['percentage'] = intval(($user / $arr * 100)).'%';
            
            //小程序二维码
            $code = $this->config();
            $res['code'] =  $this->game_img($code['code']);

            suc('',$res);
        }
    }
    
    
    /**
     * 排行榜
     * type =1好友排行 2全国排行
     */
    public function Ranking()
    {
        
        $admin_id = $this->accessData['aid'];
        $user_id = $this->accessData['uid'];
        
        $post = request()->only(['type','page','pagesize'],'post');
        
        if (empty($post['type'])){
            err('类型不能为空');
        }
        
        if (empty($post['page'])) {
            $post['page'] = 0;
        }
        if (empty($post['pagesize'])) {
            $post['pagesize'] = 5;
        }
        $page = $post['page'];
        $pagesize = $post['pagesize'];
        
        if ($post['type'] == 1){
            //获取当前用户关联ID
            $user = db('wdgs_center')->where(['admin_id'=>$admin_id,'user_id'=>$user_id])->field('relationship_id')->find();
            $where['a.relationship_id'] = $user['relationship_id'];
            $where['a.admin_id'] = $admin_id;
        }else{
            $where['a.admin_id'] = $admin_id;
        }
        
        $where['b.avatarurl'] = ['neq',""];
        $where['b.nickname'] = ['neq',""];
        $res['rank'] = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')
                ->where($where)
                ->field('b.avatarurl,b.nickname,a.paragraph,a.distance')
                ->order('a.distance desc')
                ->page($page, $pagesize)
                ->limit(500)
                ->select();

        
        $res['user'] = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')
                        ->where(['a.admin_id'=>$admin_id,'a.user_id'=>$user_id])
                        ->field('b.avatarurl,b.nickname,a.paragraph,a.distance')
                        ->find();

         
        $arr = db('wdgs_center')->alias('a')->join('user b','a.user_id = b.id','left')
                ->where($where)
                ->field('b.avatarurl,b.nickname,a.paragraph,a.distance')
                ->order('a.distance desc')
                ->select();
 
        //判断自己的名次
        foreach ($arr as $key => $val){
            if ($res['user']['nickname'] == $val['nickname']){
                $res['user']['numbers'] = $key+1;
            }
        }
                      
            
        foreach ($res['rank'] as $key => $val){
            $res['rank'][$key]['paragraph'] = "";
        }
        $res['user']['paragraph'] = "";

        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    /**
     * 分享
     * type = 1个人 2群
     */
    public function Seek_help()
    {
        $apd = "wx91018366fb42a828";
        $post = request()->only(['type','encryptedData','vi'],'post');

        if (!empty($post['encryptedData'])){
            $encryptedData = $post['encryptedData'];
        }
        if (!empty($post['vi'])){
            $vi = $post['vi'];
        }
        
        $str['user_id'] = $this->accessData['uid'];
        
        //获得分享标语
        $code = $this->config();
		$list_info = $code['share_title'];
		//群次数
		$list_number = $code['share_number'];
		//个人次数
		$my_number = $code['share_max'];

        $day = date("Y-m-d");
        $list = db('user')->field('session_key')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->find();

        //分享到群
        if ($post['type'] == 2){
            
            $memberdecrypt = $this->getDecrypt($list['session_key'],$encryptedData,$vi,$apd);

            if($memberdecrypt['openGId']){

                $group_id = $memberdecrypt['openGId'];
            }

            //获取当前群分享次数
            $res_info = db('wdgs_statistics')
                        ->where([
                                'admin_id'=>$this->accessData['aid'],
                                'user_id'=>$this->accessData['uid'],
                                'create_time'=>$day,
                                'game_id'=>$group_id,
                                'type'=>2])
                        ->count('id');

            if ($res_info >= $list_number){
                
                //增加分享次数
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['type'] = 2;
                $dat['game_id'] = $group_id;    
                $dat['create_time'] = date("Y-m-d");
                db('wdgs_statistics')->insert($dat);
                
                suc('本群分享过了',$str);
            }
            

            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['type'] = 2;
            $data['game_id'] = $group_id;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('wdgs_statistics')->insert($data);
            
            if($res){
            
                //增加分享次数
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                //增加挑战机会
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('game_number');
                suc('分享成功',$str);
            }
        
        //分享到个人    
        }elseif($post['type'] == 1){
            //获取分享次数
            $res_info = db('wdgs_statistics')
                        ->where([
                                'admin_id'=>$this->accessData['aid'],
                                'user_id'=>$this->accessData['uid'],
                                'create_time'=>$day,
                                'type'=>1])
                        ->count('id');

            if ($res_info >= $my_number){
                //增加分享次数
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['create_time'] = date("Y-m-d");
                db('wdgs_statistics')->insert($dat);
                
                suc('分享个人已满',$str);
            }
            
            
            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['type'] = 1;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('wdgs_statistics')->insert($data);
            
            if($res){
                //增加分享次数
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                //增加挑战机会
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('game_number');
                suc('分享成功',$str);
            }
        }
        
    }
    
    
    /**
     * 点击分享进入游戏
     */
    public function Seek_help_on()
    {
        $post = request()->only(['user_id','encryptedData','vi'],'post');
        
        if (!empty($post['encryptedData'])){
            $encryptedData = $post['encryptedData'];
        }
        
        if (!empty($post['vi'])){
            $vi = $post['vi'];
        }
        
        if (!empty($post['user_id'])){
            $user_id = $post['user_id'];
        }
        
        $day = date("Y-m-d");
        $apd = "wx91018366fb42a828";
        $list = db('user')->field('session_key')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->find();
        
        if (!empty($post['encryptedData']) AND !empty($post['user_id']) AND !empty($post['vi'])){
            
            $memberdecrypt = $this->getDecrypt($list['session_key'],$encryptedData,$vi,$apd);
            
            if($memberdecrypt['openGId']){
                $group_id = $memberdecrypt['openGId'];
            }
            
            //查询是否被点击过
            $arr = db('wdgs_comeshare')
                    ->where([
                        'admin_id'=>$this->accessData['aid'],
                        'cid'=>$user_id,
                        'create_time'=>$day,
                        'game_id'=>$group_id
                    ])->find();
            
            //判断次数
            $number = db('wdgs_comeshare')->where(['admin_id'=>$this->accessData['aid'],'cid'=>$user_id,'create_time'=>$day])->count();
            
            //配置次数
            $my_number = $this->config();
            
            if (!$arr  && $number<$my_number['game_wuhui_number']){
                //增加记录
                $data['admin_id'] = $this->accessData['aid'];
                $data['cid'] = $user_id;
                $data['game_id'] = $group_id;
                $data['create_time'] = $day;
                
                $info = db('wdgs_comeshare')->insert($data);
                db('wdgs_center')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('game_wudi_number');
                
                suc('OK');
            }else{
                suc('OK');
            }
        }
        
        suc('OK');
        
    }
    
    /**
     * 使用无敌卡
     */
    public function Wudi()
    {
        $res = db('wdgs_center')
            ->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])
            ->setInc('game_wudi_use');
        
        //获得无敌卡时间
        $wudi_time = $this->config();
        $times['game_time'] = $wudi_time['game_time'];

        if ($res){
            suc('',$times);
        }
    }
    
    
    /**
     * 分享获取图片和文本
     */
    public function Shera()
    {
        $res = $this->config();
        
        $arr['img'] = $this->game_img($res['share_img']);
        $arr['title'] = $res['share_title'];
        
        suc('',$arr);
    }
    
    ##没有次数分享
    public function shera_fl()
    {
        $config = $this->config();
        
        $res['one'] = $config['game_over_one'];
        $res['two'] = $config['game_over_two'];
        $res['therr'] = $config['game_over_therr'];
        
        suc('',$res);
    }
    
    ##其他游戏URL
    public function Gmae_url()
    {
        $image = $this->config();
        
        $res['image'] = $this->game_img($image['game_image']);
        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}